# --------------------------------------------------------------------------------------------------------------------
# Project:             Digital Payment System (DPS)
# Website:             http://code.google.com/p/dps-x509/
# Purpose of document: This file generates the pydoc because pydoc isn't smart enough to be recursive
# --------------------------------------------------------------------------------------------------------------------
import subprocess
import os.path
import re

def runPydoc():
  fs = []
  for root, dirs, files in os.walk("."):
    for f in files:
      if f.endswith(".py") and not f.startswith("__") and not f == __file__:
        fs.append("dps."+f.rstrip(".py"))

  os.chdir("..")
  for f in fs:
    subprocess.call(["c:/Python25/Python.exe", "c:/Python25/Lib/pydoc.py", "-w", f])

def runCodeCounter(dir):
  fs = []
  for root, dirs, files in os.walk(dir):
    for f in files:
      if f.endswith(".py") and not f == __file__:
        fs.append(dir+f)

  nlines = ncode = ncomments = nblank = nfiles = 0
  for file in fs:
    lines = []
    try:
      lines = open(file).readlines()
      nfiles += 1
    except:
      pass
    
    isInComment = False
    isInStringLit = False
    for l in lines:
      if re.match("^\s*$", l):
        nblank += 1
      elif isInComment:
        ncomments += 1
        if re.match('^.*"""\s*$', l):
          isInComment = False
      elif isInStringLit:
        ncode += 1
        if re.match('^.*""".*$', l):
          isInStringLit = False
      else:
        if re.match('^\s*#.*$', l):
          ncomments += 1
        elif re.match('^\s*""".*"""\s*$', l):
          ncomments += 1
        elif re.match('^\s*\S+.*""".*""".*$', l):
          ncode += 1
        elif re.match('^\s*""".*$', l):
          ncomments += 1
          isInComment = True
        elif re.match('^\s*\S+.*""".*$', l):
          ncode += 1
          isInStringLit = True
        else:
          ncode += 1
      nlines += 1
    assert(not isInComment)
    assert(not isInStringLit)

  print "dir:%s\tnlines:\t%d\tncode:\t%d\tncomments:\t%d\tnblank:\t%d\tnfiles:\t%d\n" % \
  (dir, nlines, ncode, ncomments, nblank, nfiles) 
    

runCodeCounter("../dps/") 
runCodeCounter("../../../roulette/trunk/")
runCodeCounter("../../../registrar/trunk/")